package com.leetcode.merci.lc002;

import com.leetcode.merci.ListNode;

import java.util.List;
import java.util.Stack;
import java.util.Vector;

/**
 * Created by bowen on 2017/8/2.
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head=new ListNode(0);
        ListNode now=head;
        int mark=0;
        while(l1!=null||l2!=null){
            int x=0;
            if(l1!=null&&l2!=null){
                x=l1.val+l2.val+mark;
            }
            else if(l1!=null){
                x=l1.val+mark;
            }
            else if(l2!=null){
                x=l2.val+mark;
            }
            mark=0;
            if(x>=10){
                mark=1;
                x=x-10;
            }
            ListNode t=new ListNode(x);

            now.next=t;
            now=now.next;
            l1=l1.next;
            l2=l2.next;
        }
        if(mark!=0){
            now.next=new ListNode(mark);
        }
        return head.next;
    }

    public static void main(String[] args) {
        ListNode l1=new ListNode(2);
        l1.next=new ListNode(4);
        l1.next.next=new ListNode(5);
        ListNode l2=new ListNode(5);
        l2.next=new ListNode(6);
        l2.next.next=new ListNode(4);
        Solution solution=new Solution();
        solution.addTwoNumbers(l1,l2);
    }
}
